package cn.hist.hfp.controller;

import cn.hist.hfp.RoleType;
import cn.hist.hfp.entity.BusinessProvider;
import cn.hist.hfp.service.BusinessProviderService;
import cn.hist.hfp.util.Authentication;
import cn.hist.hfp.util.MyResponse;
import cn.hist.hfp.vo.BusinessProviderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/provider")
public class BusinessProviderController {

    private final
    BusinessProviderService businessProviderService;

    private final
    Authentication authentication;

    private final MyResponse<BusinessProvider> myResponse;

    @Autowired
    public BusinessProviderController(BusinessProviderService businessProviderService,
                                      Authentication authentication, MyResponse<BusinessProvider> myResponse) {
        this.businessProviderService = businessProviderService;
        this.authentication = authentication;
        this.myResponse = myResponse;
    }

    @PostMapping
    public ResponseEntity<BusinessProvider> create(@RequestBody BusinessProvider businessProvider, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        return myResponse.response(201, authentication.refreshToken(token), businessProviderService.create(businessProvider));
    }

    @GetMapping("/{id}")
    public BusinessProvider findOne(@PathVariable int id) {
        return businessProviderService.findOne(id);
    }

    @PutMapping
    public ResponseEntity<BusinessProvider> update(@RequestBody BusinessProvider businessProvider, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        return myResponse.response(200, authentication.refreshToken(token), businessProviderService.update(businessProvider));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> delete(@PathVariable int id, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        MyResponse<Integer> myResponse = new MyResponse<>();
        return myResponse.response(200, authentication.refreshToken(token),  businessProviderService.delete(id) );
    }

    @GetMapping("/page/{page}")
    public List<BusinessProviderVO> findSome(@RequestParam String city, @RequestParam int business_id, @PathVariable int page) {
        return businessProviderService.findSome(city, business_id, page);
    }

    @GetMapping("/page")
    public int numberOfPage(@RequestParam String city, @RequestParam int business_id) {
        return businessProviderService.numberOfPages(city, business_id);
    }

    @PostMapping("/{business_provider_id}/add/{business_id}")
    public ResponseEntity<BusinessProvider> addBusiness(@PathVariable int business_provider_id, @PathVariable int business_id, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        return myResponse.response(200, authentication.refreshToken(token),
                businessProviderService.addBusiness(business_provider_id, business_id));
    }

    @PostMapping("/{business_provider_id}/delete/{business_id}")
    public ResponseEntity<BusinessProvider> deleteBusiness(@PathVariable int business_provider_id, @PathVariable int business_id, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        return myResponse.response(200, authentication.refreshToken(token),
                businessProviderService.deleteBusiness(business_provider_id, business_id));
    }
}
